package com.chushouya.product.dao.repository;

import com.general.framework.core.lang.Lists;
import com.general.framework.core.lang.Longs;
import com.general.framework.core.lang.Strings;
import com.general.framework.core.schema.PageResult;
import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.product.dao.entity.ProductAttrEntity;
import com.chushouya.product.dao.mapper.ProductAttrEntityMapper;
import com.chushouya.product.dto.admin.product.query.ProductAttrQuery;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

@Repository
public class ProductAttrRepository extends BaseRepository<ProductAttrEntityMapper, ProductAttrEntity, Long> {
    
    public List<ProductAttrEntity> selectList(ProductAttrQuery query) {
        Example example = buildExample(query);
        return this.selectByExample(example);
    }

    public PageResult<ProductAttrEntity> selectPageResult(ProductAttrQuery query) {
        Example example = this.buildExample(query);
        return this.selectPage(query.getPageRequest(), example);
    }
    
    public ProductAttrEntity selectOne(ProductAttrQuery query) {
        Example example = buildExample(query);
        return this.selectFirstOneByExample(example);
    }

    private Example buildExample(ProductAttrQuery query) {
        Example example = new Example(ProductAttrEntity.class);
        Example.Criteria criteria = example.createCriteria();
        if (Longs.notNullAndZero(query.getProductAttrId())) {
            criteria.andEqualTo("productAttrId", query.getProductAttrId());
        }
        if (Objects.nonNull(query.getProductId())) {
            criteria.andEqualTo("productId", query.getProductId());
        }
        if (Longs.notNullAndZero(query.getAttrId())) {
            criteria.andEqualTo("attrId", query.getAttrId());
        }
        if (Strings.isNotEmpty(query.getAttrName())) {
            criteria.andLike("attrName", "%" + query.getAttrName() + "%");
        }
        example.orderBy("sort").asc();
        return example;
    }
    
    public List<ProductAttrEntity> selectListByProductId(Long productId) {
        ProductAttrQuery query = new ProductAttrQuery();
        query.setProductId(productId);
        return selectList(query);
    }
    
    /**
     * 根据商品ID删除所有属性
     */
    public void deleteByProductId(Long productId) {
        Example example = new Example(ProductAttrEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("productId", productId);
        this.deleteByExample(example);
    }
    
    /**
     * 批量插入商品属性
     */
    public void batchInsert(List<ProductAttrEntity> attrEntities) {
        if (Lists.isEmpty(attrEntities)) {
            return;
        }
        
        // 分批插入，每批100条记录
        int batchSize = 100;
        for (int i = 0; i < attrEntities.size(); i += batchSize) {
            int endIndex = Math.min(i + batchSize, attrEntities.size());
            List<ProductAttrEntity> batchList = attrEntities.subList(i, endIndex);
            
            // 使用tk.mybatis的insertList方法进行批量插入
            this.insertList(batchList);
        }
    }
}
